<?php

namespace app\beautiful\model\order;

use app\common\model\CommonModel;




class OrderRecordModel extends CommonModel{

    protected  $name = 'order_record';

    protected  $pk   = 'record_id';
    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function list($where,$order,$page=0,$limit=0,$field='*'){
        $query = self::alias('r')
            ->leftjoin('account a','a.account_id=r.operate_id')
            ->where(['r.is_delete'=>0])
            ->when(isset($where['shop_id']),function ($query) use ($where){
                $query->where('r.shop_id','=',$where['shop_id']);
            })->when(isset($where['order_id']),function ($query) use ($where){
                $query->where('r.order_id','=',$where['order_id']);
            })->when(isset($where['buy_order_id']),function ($query) use ($where){
                $query->where('r.buy_order_id','=',$where['buy_order_id']);
            })
            ->field($field);
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $info = NULL;
                if($v['buy_order_id']){
                    if($v['status'] == 1){
                        $info = '全部退款';
                    }
                    if($v['status'] == 2){
                        $info = '部分退款';
                    }
                }
                if($v['order_id']){
                    if($v['status'] == 1){
                        $info = '全部退款';
                    }
                    if($v['status'] == 2){
                        $info = '部分退款';
                    }
                    if($v['status'] == 3){
                        $info = '确认核销';
                    }
                    if($v['status'] == 4){
                        $info = '确认发货';
                    }
                    if($v['status'] == 5){
                        $info = '订单完成';
                    }
                    if($v['status'] == 6){
                        $info = '同意退款';
                    }
                    if($v['status'] == 7){
                        $info = '拒绝退款';
                    }
                    if($v['status'] == 8){
                        $info = '配送完成';
                    }
                    if($v['status'] == 9){
                        $info = '退单';
                    }
                }
                $list[$k]['info'] = $info;

            }
        }
        return compact('count','list');
    }

}



